﻿using LightORM.Core.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Data.SqlClient;
using System.Collections.Generic;
using TxWeb.Webgame.Model;
using System.IO;
using System.Diagnostics;
using System.Linq;
namespace UnitTest
{


    /// <summary>
    ///This is a test class for DefaultDataFacadeTest and is intended
    ///to contain all DefaultDataFacadeTest Unit Tests
    ///</summary>
    [TestClass()]
    public class DefaultDataFacadeTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        public static IEnumerable<TerrainGridModel> GetTerrainGridsByRoute(string route)
        {
            string connectstring = "Data Source=.;Initial Catalog=WebGame3;Integrated Security=True"; // TODO: Initialize to an appropriate value
            DefaultDataFacade target = new DefaultDataFacade(connectstring);
            //if (string.IsNullOrEmpty(route))
            //{
            //    return new List<TerrainGridModel>();
            //}
            //string sql = "SET NOCOUNT ON select * from wg_terraingrid with(nolock) where GridID in ({0});SET NOCOUNT OFF ";
            //sql = String.Format(sql, route.Replace("|", ","));

            return target.Select<TerrainGridModel>("select top 10 * From wg_terrainGrid");
        }

        private static void SelectManyWithReflectionCache()
        {
            File.Delete(@"d:\new.txt");
            for (int i = 0; i < 10000; i++)
            {
                Stopwatch st = new Stopwatch();
                Random r = new Random(DateTime.Now.Millisecond);
                string route = "23|234|345|56|345|6785|234234|345676|" + r.Next(0, 640000).ToString();
                st.Start();
                var t = GetTerrainGridsByRoute(route);
                string s = string.Format("{0}", st.ElapsedMilliseconds) + Environment.NewLine;
                File.AppendAllText(@"d:\new.txt", s);
            }
        }

        [TestMethod]
        public void StoredProcedureTest()
        {

            //DefaultDataFacade d=new DefaultDataFacade(
            string connectstring = System.Configuration.ConfigurationManager.ConnectionStrings["UnitTest.Properties.Settings.WebGame3ConnectionString"].ConnectionString; ; // TODO: Initialize to an appropriate value
            DefaultDataFacade target = new DefaultDataFacade(connectstring); // TODO: Initialize to an appropriate value
            string spname = "SPTest";
            SqlParameter[] parameters = ParameterCache.ResolveSpParameters(connectstring, spname);
          //  parameters[0].Direction = System.Data.ParameterDirection.Output;

            //   parameters[1].Direction = System.Data.ParameterDirection.Output;
            int t = 0;
            //  t = target.ExecuteStoredProdedure<int>(spname, parameters);
            var p = target.ExecuteStoredProdedureWithResultSet(spname, parameters);


            Assert.AreEqual<int>(500, t);
        }

        [TestMethod()]
        public void SingleTest()
        {
            //DefaultDataFacade d=new DefaultDataFacade(
            string connectstring = System.Configuration.ConfigurationManager.ConnectionStrings["UnitTest.Properties.Settings.WebGame3ConnectionString"].ConnectionString; ; // TODO: Initialize to an appropriate value
            DefaultDataFacade target = new DefaultDataFacade(connectstring); // TODO: Initialize to an appropriate value
            string query = "select top 10 * From wg_terrainGrid where gridid=10";
            var t = target.Single<TerrainGridModel>(query);

            Assert.IsNotNull(t);
            query = "select top 10 * From wg_terrainGrid where gridid<0";
            t = target.Single<TerrainGridModel>(query);
            Assert.IsNull(t);
        }

        [TestMethod()]
        public void SelectTest()
        {

            SelectManyWithReflectionCache();
            SelectManyWithReflectionCache();
            SelectManyWithReflectionCache();

            string connectstring = System.Configuration.ConfigurationManager.ConnectionStrings["UnitTest.Properties.Settings.WebGame3ConnectionString"].ConnectionString; ; // TODO: Initialize to an appropriate value
            DefaultDataFacade target = new DefaultDataFacade(connectstring); // TODO: Initialize to an appropriate value
            string query = "select top 10 * From wg_terrainGrid"; // TODO: Initialize to an appropriate value
            SqlParameter[] parameters = null; // TODO: Initialize to an appropriate value
            IEnumerable<TerrainGridModel> expected = null; // TODO: Initialize to an appropriate value
            IEnumerable<TerrainGridModel> actual;
            actual = target.Select<TerrainGridModel>(query, parameters);
            //target.SelectDictionary<k,v>(.Single<testc>(
            string s = "";
            actual = target.Select<TerrainGridModel>(query, parameters);
            s = "OK";
            actual = target.Select<TerrainGridModel>(query, parameters);

            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }
    }
}
